<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="reload">
            <div class="layui-form-item">

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="type">
                            <option value="">课程属性</option>
                            {volist name="type" id="vo" key="k"}
                            <option value="{$k}">{$vo}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="module">
                            <option value="">课程类型</option>
                            {volist name="module" id="vo" key="k"}
                            <option value="{$k}">{$vo}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="seminar_topic">
                            <option value="">课程分类</option>
                            {volist name="seminar_topic" id="vo"}
                            <option value="{$vo.id}">{$vo.e_title}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="english">
                            <option value="">课程语言</option>
                            {volist name="english" id="vo" key="k"}
                            <option value="{$k}">{$vo}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="prerequisite">
                            <option value="">先修课程</option>
                            {volist name="prerequisite" id="vo"}
                            <option value="{$vo.courseid}">{$vo.e_seminar}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="keys" placeholder="关键字" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="import">
        <i class="layui-icon layui-icon-file"></i>
        导入
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-sm" id="more_{{d.id}}"><i class="layui-icon layui-icon-triangle-d"></i></button>
</script>
<script type="text/html" id="create-time">
    {{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery','common', 'dropdown'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let dropdown = layui.dropdown;
        let common = layui.common;

        let cols = [
            [{
                type: 'checkbox',
                fixed: 'left',
                width:'5%'
            },
                {
                    title: 'ID',
                    field: 'id',
                    align: 'center',
                    fixed: 'left',
                    width:'10%'
                },
                {
                    title: 'Courseid',
                    field: 'courseid',
                    align: 'center',
                    fixed: 'left',
                    width:'10%'
                },
                {
                    title: '课程名称',
                    field: 'seminar',
                    align: 'center',
                    fixed: 'left',
                    width:'20%'
                },
                {
                    title: '课程分类',
                    field: 'seminar_topic',
                    align: 'center',
                    width:'15%'
                },
                {
                    title: '课程属性',
                    field: 'type_name',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '课程类型',
                    field: 'module_name',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '课程语言',
                    field: 'english_name',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '创建时间',
                    field: 'add_time',
                    align: 'center',
                    templet: '#create-time',
                    width:'15%'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width:'10%'
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Course/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }],
            done: function(res, curr, count) {
                for (var i = 0; i < res.data.length; i++) {
                    dropdown.render({
                        elem: '#more_' + res.data[i].id,
                        data: [{
                            title: '修改',
                            id: res.data[i].id,
                            type: 1,
                        }, {
                            title: '可见范围',
                            id: res.data[i].id,
                            courseid: res.data[i].courseid,
                            type: 2
                        }, {
                            title: '软推荐',
                            id: res.data[i].id,
                            courseid: res.data[i].courseid,
                            type: 3
                        }],
                        id: '#more_' + res.data[i].id,
                        click: function(obj) {
                            if (obj.type == 1) {

                                layer.open({
                                    type: 2,
                                    title: '修改',
                                    shade: 0.1,
                                    area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                                    content: '/Course/edit?id='+obj.id
                                });

                            } else if (obj.type == 2) {
                                parent.layui.admin.addTab(obj.courseid, obj.courseid+"可见范围", '/Course/recommend?courseid='+obj.courseid+'&type=1')
                            } else if(obj.type == 3) {
                                parent.layui.admin.addTab('22'+obj.id, obj.courseid+"软推荐", '/Course/recommend?courseid='+obj.courseid+'&type=2')
                            }
                        }
                    });
                }
            }
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });


        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'import') {
                window.import();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'export') {
                window.export();
            }
        });


        window.add = function() {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Course/add'
            });
        }


        window.import = function() {
            layer.open({
                type: 2,
                title: '导入',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Course/import'
            });
        }

        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出课程数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('export')}?type="+field.type+"&module="+field.module+"&seminar_topic="+field.seminar_topic+"&english="+field.english+"&prerequisite="+field.prerequisite+"&keys="+field.keys, "_blank");
            });
        };



        window.batchRemove = function(obj) {

            var checkIds = common.checkField(obj,'id');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除选中的课程吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Course/delete",
                    data:{id:checkIds},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
